package com.ruoyi.common.utils.docx;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;

import java.io.File;

/**
 * @ClassName DocxFmtConvert
 * @Description doc转换docx
 * @Author luguanqing
 * @Date 2020/11/27 11:17
 * @Version 1.0
 */
public class DocxFmtConvert {

    /** doc格式 */
    private static final int DOC_FMT = 0;

    /** docx格式 */
    private static  final  int DOCX_FMT = 12;

    /***jacob操作office内部对应代码 */
    /*** xlsx 格式 */
    private static final int XLSX_FMT = 51;

    /*** xls 格式 */
    private static final int XLS_FMT = 1;


    /**
     * 转换doc文件
     */
    public static File convertDocFmt(String srcPath, int fmt){
        ComThread.startMainSTA();
        //实例化ComThread线程与ActiveXComponent
        System.out.println("doc转docx开始启动...");
        long start = System.currentTimeMillis();
        ComThread.InitMTA(true);
        ActiveXComponent app = new ActiveXComponent("Word.Application");
        //文档隐藏时进行应用操作
        app.setProperty("Visible", new Variant(false));
        File file = null;
        try{
            //实例化模板Document对象
            Dispatch document = app.getProperty("Documents").toDispatch();
            //打开Document进行另存为操作
            System.out.println("转换文档为docx：" + srcPath);
            Dispatch doc = Dispatch.invoke(document, "Open", Dispatch.Method,
                    new Object[] { srcPath, new Variant(true), new Variant(true) }, new int[1]).toDispatch();
            Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] { srcPath + "x", new Variant(fmt) }, new int[1]);
            Dispatch.call(doc, "Close", new Variant(false));
            file = new File(srcPath + "x");
            long end = System.currentTimeMillis();
            System.out.println("转换完成，用时：" + (end - start) + "ms");
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            //释放线程与ActiveXComponent
            app.invoke("Quit", new Variant[] {});
            ComThread.Release();
        }
        return file;
    }


    /**
     * 根据格式类型转换xls文件
     *
     * @param srcPath  xls path 源文件
     * @return the file
     * @throws Exception the exception
     */
    public static File convertXlsfmt(String srcPath, int fmt) throws Exception {
        // 实例化ComThread线程与ActiveXComponent
        ComThread.InitSTA();
        //实例化ComThread线程与ActiveXComponent
        System.out.println("doc转docx开始启动...");
        long start = System.currentTimeMillis();
        ActiveXComponent wordApp = new ActiveXComponent("Excel.Application");
        try {
            // 文件隐藏时进行应用操作
            wordApp.setProperty("Visible", new Variant(false));
            // 实例化模板Workbooks对象
            Dispatch workbooks = wordApp.getProperty("Workbooks").toDispatch();
            // 打开Workbooks进行另存为操作
            Dispatch doc = Dispatch.invoke(workbooks, "Open", Dispatch.Method, new Object[]{srcPath, new Variant(true), new Variant(true)}, new int[1]).toDispatch();
            Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[]{srcPath  + "x", new Variant(fmt)}, new int[1]);
            Dispatch.call(doc, "Close", new Variant(false));
            long end = System.currentTimeMillis();
            System.out.println("转换完成，用时：" + (end - start) + "ms");
            return new File(srcPath  + "x");
        } catch (Exception e) {
            throw e;
        } finally {
            // 释放线程与ActiveXComponent
            wordApp.invoke("Quit", new Variant[]{});
            ComThread.Release();
        }
    }



    //public static void main(String[] args) throws Exception {
    //    convertXlsfmt("D:\\ruoyi\\uploadPath\\report\\template\\2023\\06\\07/92403849-3802-48c3-804b-72a90d8d6262.xls",XLSX_FMT);
    //}
}
